package br.ufpb.sistcomp.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import br.ufpb.sistcomp.R;
import br.ufpb.sistcomp.database.FacadeDatabase;
import br.ufpb.sistcomp.database.InformationDatabase;
import br.ufpb.sistcomp.entity.Discipline;
import br.ufpb.sistcomp.entity.Semester;
import br.ufpb.sistcomp.entity.Teacher;
import br.ufpb.sistcomp.persistence.DisciplinePersistence;
import br.ufpb.sistcomp.persistence.PointPersistence;
import br.ufpb.sistcomp.persistence.SemesterPersistence;
import br.ufpb.sistcomp.persistence.TeacherPersistence;
import br.ufpb.sistcomp.util.Message;

public class MainActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		FacadeDatabase facadeDatabase = new FacadeDatabase(
				getApplicationContext());
		/*
		 * Test create Database and tables.
		 */

		facadeDatabase.createDatabaseAndTables();

		/*
		 * Test Create, Delete Tables.
		 */

		// facadeDatabase.runScript(InformationDatabase.SCRIPT_DELETE_ALL_TABLE);

		// facadeDatabase.runScript(InformationDatabase.SCRIPT_CREATE_TABLE_DISCIPLINES);
		// facadeDatabase.runScript(InformationDatabase.SCRIPT_CREATE_TABLE_POINTS);
		// facadeDatabase.runScript(InformationDatabase.SCRIPT_CREATE_TABLE_SEMESTERS);
		// facadeDatabase.runScript(InformationDatabase.SCRIPT_CREATE_TABLE_TEACHERS);

		// facadeDatabase.runScript(InformationDatabase.SCRIPT_DELETE_TABLE_DISCIPLINES);
		// facadeDatabase.runScript(InformationDatabase.SCRIPT_DELETE_TABLE_POINTS);
		// facadeDatabase.runScript(InformationDatabase.SCRIPT_DELETE_TABLE_SEMESTERS);
		// facadeDatabase.runScript(InformationDatabase.SCRIPT_DELETE_TABLE_TEACHERS);

		// facadeDatabase.runScript(InformationDatabase.SCRIPT_CREATE_ALL_TABLE);

		/*
		 * Create Objects
		 */

		Teacher teacher = new Teacher("Diego1", "diego@diegosousa.com",
				"3421-1048", "3421-1048");
		Teacher teacher2 = new Teacher("Diego2", "diego2@diegosousa.com",
				"3422-1048", "3422-1048");
		Teacher teacher3 = new Teacher("Diego3", "diego3@diegosousa.com",
				"3423-1048", "3423-1048");

		Semester semester = new Semester("2008.1");
		Semester semester2 = new Semester("2009.1");
		Semester semester3 = new Semester("2010.1");

		Discipline discipline = new Discipline("Calculo1", "10:00");
		Discipline discipline2 = new Discipline("Calculo2", "11:00");
		Discipline discipline3 = new Discipline("Calculo3", "12:00");

		TeacherPersistence teacherPersistence = TeacherPersistence
				.getInstance(getApplicationContext());
		SemesterPersistence semesterPersistence = SemesterPersistence
				.getInstance(getApplicationContext());
		DisciplinePersistence disciplinePersistence = DisciplinePersistence
				.getInstance(getApplicationContext());
		PointPersistence pointPersistence = PointPersistence
				.getInstance(getApplicationContext());

		/*
		 * test insert data in tables.
		 */

		// teacherPersistence.insertTeacher(teacher);
		// teacherPersistence.insertTeacher(teacher2);
		// teacherPersistence.insertTeacher(teacher3);
		//
		// semesterPersistence.insertSemester(semester);
		// semesterPersistence.insertSemester(semester2);
		// semesterPersistence.insertSemester(semester3);
		//
		// disciplinePersistence.insertDiscipline(discipline, teacher,
		// semester);
		// disciplinePersistence
		// .insertDiscipline(discipline2, teacher2, semester2);
		// disciplinePersistence
		// .insertDiscipline(discipline3, teacher3, semester3);
		//
		// pointPersistence.insertPoint(discipline, 7.2);
		// pointPersistence.insertPoint(discipline, 4.0);
		// pointPersistence.insertPoint(discipline2, 5.0);
		// pointPersistence.insertPoint(discipline2, 2.0);
		// pointPersistence.insertPoint(discipline3, 2.0);
		// pointPersistence.insertPoint(discipline3, 9.5);

		/*
		 * Test update data in tables.
		 */

		// Teacher teacherUpdate = teacherPersistence.locateTeacher("Diego2");
		// teacherUpdate.setName("Jafet");
		// teacherPersistence.updateTeacher(teacherUpdate);
		//
		// Semester semesterUpdate =
		// semesterPersistence.locateSemester("2009.1");
		// semesterUpdate.setName("2011.2");
		// semesterPersistence.updateSemester(semesterUpdate);
		//
		// Discipline disciplineUpdate =
		// disciplinePersistence.locateDisciplines("Calculo2");
		// disciplineUpdate.setMedia(4.0);
		// disciplinePersistence.updateDisciplines(disciplineUpdate);
		//
		// pointPersistence.updatePoint();// Falta implementa.

		/*
		 * Test remove data in tables.
		 */

		// teacherPersistence.removeTeacher("Diego2");
		// pointPersistence.removePoint(discipline2, 7.2); //Falta Implementar;
		// semesterPersistence.removeSemester("2009.1");
		// disciplinePersistence.removeDisciplines("Calculo2");

		/*
		 * Test Update DataBase ps: Create a base backup, migrates data from
		 * tables and deletes the original base.
		 */

		// facadeDatabase.upgrade(2);

		facadeDatabase.closeDatabase();

		Message.displayMessage("Aplicação executada com Sucesso...",
				"Running...", this);
	}
}
